<?
final class wordCloud
{
    private $maxWords = 20;
    private $maxLines = 5;
    private $nowWord = 0;
    private $nowLines = 0;
    private $wordsArray = array();
    private $count = 0;
    private $perLine = 10;
    private $nowElement = 1;
   
    function __construct( $words = false, $ves , $check, $maxWords = 20, $maxLines = 5, $perLine = 10 )
    {
     $this->maxWords = $maxWords;
     $this->maxLines = $maxLines;
     $this->perLine  = $perLine;
     $this->checks = $check;
//     print_r($words);
     
     if ( is_array( $words ) && $words !== false )
        {
            $cwords=count($words);
            for ($j=0;$j<$cwords;$j++)
            {
            $key=$words[$j];
            $value=$ves[$j];
            $this->addWord($key, $value );
            }
            /*            
            foreach ( $words as $key => $value )
            {
                echo "$key, $value <br>";
                $this->addWord( $value );
            }
            */
        }
    }

    function addWord( $word, $value = 1 )
    {
     $word = strtolower( $word );
   
     if ( array_key_exists( $word, $this->wordsArray ) )
     {
         $this->wordsArray[ $word ] += $value;
     }
     else
     {
            $this->wordsArray[ $word ] = $value;       
     }
   
        return $this->wordsArray[ $word ];
    }

    function shuffleCloud()
    {
     $keys = array_keys( $this->wordsArray );
        shuffle( $keys );

        if ( is_array( $keys ) && count( $keys ) )
        {
         $tmpArray        = $this->wordsArray;
         $this->wordsArray = array( );
           
         foreach ( $keys as $key => $value )
         {
                $this->wordsArray[ $value ] = $tmpArray[ $value ];   
         }
        }
    }

    function getCloudSize()
    {
        return array_sum( $this->wordsArray );
    }

    function getClassFromPercent( $percent )
    {
        if ($percent >= 99)
        $class = 1;
        else if ( $percent >= 90 )
        $class = 2;
        else if ( $percent >= 80 )
        $class = 3;
        else if ( $percent >= 70 )
        $class = 4;
        else if ( $percent >= 60 )
        $class = 5;
        else if ( $percent >= 50 )
        $class = 6;
        else if ( $percent >= 30 )
        $class = 7;
        else if ( $percent >= 20 )
        $class = 8;
        else if ( $percent >= 10 )
        $class = 9;
        else
        $class = 0;

     return $class;
    }
   
    function showCloud( $returnType = "html" )
    {
    $this->shuffleCloud();
    $this->max = max( $this->wordsArray );
    $mymin = min( $this->wordsArray ); 
    $countw = count( $this->wordsArray );
    
     echo  "<span class='word size5'><nobr>";
     //echo "VVVV ".$this->max ." - $mymin - $countw <br>"; 
                 //echo '';
     echo "</nobr></span>\n";
    
    
    
        if ( is_array( $this->wordsArray ) )
        {          
         $return = ( $returnType == "html" ? "" : ( $returnType == "array" ? array( ) : "" ) );
         foreach ( $this->wordsArray as $word => $popularity )
         {
             $sizeRange = $this->getClassFromPercent( ( $popularity / $this->max ) * 100 );
             if ( $returnType == "array" )
             {
                    $return[ $word ][ 'word' ]        = $word;
                 $return[ $word ][ 'sizeRange' ] = $sizeRange;
                 if ( $currentColour )
                     $return[ $word ][ 'randomColour' ] = $currentColour;
             }
             else if ( $returnType == "html" )
             {
                 if( $this->nowLine  == $this->maxLines ) return;
                 if( $this->nowWord  == $this->maxWords ) return;
                 
                 echo  "<span class='word size{$sizeRange}'><nobr>";
                 if ($this->check==1)
                 {echo '<input type="checkbox" name="v">';}
                 echo $word.'('.$popularity.')';
                 //echo '';
                 echo "</nobr></span>\n";
                 
                    if( $this->nowElement == $this->perLine )
                    {
                        print  "\n<br />\n\n";
                        $this->nowElement = 0;
                        $this->nowLine++;
                    }
                    
                    $this->nowElement++;
                    $this->nowWord++;
             }
         }
              
     return $return;
     }   
    }
}

if ($z==999999)
{

 /*
 $randomWords = array(
    "webmasterworld", "vingrad", "Computer", "C#",  "PC", "sex",  "music", "music", "music", "music", "sex",  "PHP", "C", "vingrad", "XHTML", "sex", "C#",  "eminem", "programming", "forums", "sex",  "webmasterworld", "vingrad", "sex", "C#", 
    "email", "sex",  "vingrad", "C#", "C#", "C#", "C#", "C#", "C#", "C#",  "forums", "C#",  "Computer", "GTA", "css", "mysql", "sql", "css", "mysql", "C#",  "sql", "gta4kv for vingrad", "gta4kv for vingrad", "gta4kv for vingrad", "gta4kv for vingrad", "gta4kv for vingrad", "gta4kv for vingrad", "gta4kv for vingrad", "gta4kv for vingrad", 
    "forums", "internet", "class", "vingrad", "object", "sex",  "C#",  "method", "music", "music", "music", "music", "gui", "sex",  "vingrad", "encryption"
    );
/**/    
    
     $randomWords = array(
    "webmasterworld", "vingrad", "Computer",
    "email", "sex",  "C#", 
    "forums", "internet", "class"
    );
  /*  */
    
     $randomVes = array(
    300, 1000, 100,
    280, 500,  56, 
    1580, 11, 450
    );

    $cloud = new wordCloud( $randomWords, $randomVes,0, 250, 3, 5 );
    $cloud->shuffleCloud(); 

echo '<div style="background-color:#ffc;width: 500px; height: 200px;">';       
echo $cloud->showCloud();
echo '</div>';
}
?>